<?php
class Zendhlt_Form_Decorator_Captcha extends Zend_Form_Decorator_Captcha {

     public function render($content) {
        $element = $this->getElement();
        if (!method_exists($element, 'getCaptcha')) {
            return $content;
        }

        $view = $element->getView();
        if (null === $view) {
            return $content;
        }
        
        $name = $element->getFullyQualifiedName();

        $hiddenName = $name . '[id]';
        $textName = $name . '[input]';

        $label = $element->getDecorator("Label");
        if ($label) {
            $label->setOption("id", $element->getId() . "-input");
        }

        $placement = $this->getPlacement();
        $separator = $this->getSeparator();

        $captcha = $element->getCaptcha();
        $markup = $captcha->render($view, $element);
        $hidden = $view->formHidden($hiddenName, $element->getValue(), $element->getAttribs());
        $text = $view->formText($textName, '', $element->getAttribs());


        // CHANGE THE ORDER OF ELEMENTS AND ADD THE div AND span TAGS.

        switch ($placement) {
            case 'PREPEND':
                $content = '<div class="captcha"><span>' . $markup . $hidden . '</span></div><div id="refreshcaptcha"></div>' .
                		'<div><span>' . $text . '</span></div>' .
                        $separator . $content;
                break;
            case 'APPEND':
            default:
                $content = $content . $separator .
                        '<div class="captcha"><span>' . $markup . $hidden . '</span><div id="refreshcaptcha"></div></div>' .
                		'<div><span>' . $text . '</span></div>';
        }

        return $content;
    }

}